package com.ihd.ihardwaretools.view.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.view.View;

import com.ihd.ihardwaretools.R;
import com.ihd.ihardwaretools.databinding.ItemListBinding;
import com.ihd.ihardwaretools.databinding.ItemPopupKeyValueBinding;
import com.ihd.ihardwaretools.pojo.ListRes;
import com.ihd.ihardwaretools.view.model.MRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.LiveDataBus.LiveDataBus;
import cn.wowjoy.commonlibrary.adapter.CommonAdapter;


public class MainViewModel extends AndroidViewModel {

    private MRepository mLoginRepository;
    private Application mApplication;

    public MainViewModel(@NonNull Application application) {
        super(application);
        mApplication = application;
    }

    private ObservableArrayList<String> mWhoType = new ObservableArrayList<>();
    public CommonAdapter mWhoAdapter = new CommonAdapter<String, ItemPopupKeyValueBinding>(R.layout.item_popup_key_value, mWhoType, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            LiveDataBus.get().with("1").setValue(position);
        }
    }) {
    };

    public void setActions() {
//        mWhoType.add("修改密码");
        mWhoType.add("退出登录");
    }

    private ObservableArrayList<ListRes.DataBean.ListBean> mConcerned = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<ListRes.DataBean.ListBean, ItemListBinding>(R.layout.item_list, mConcerned, null) {
        @Override
        public void convert(ItemListBinding mDataBinding, ListRes.DataBean.ListBean test_concern, int position) {
        }
    };

    public void setList(List<ListRes.DataBean.ListBean> rd) {
        mConcerned.clear();
        if (rd != null && rd.size() > 0)
            mConcerned.addAll(rd);
    }

    public void bind(String data, String customerId, String productionLine) {
        if (mLoginRepository == null)
            mLoginRepository = new MRepository();
        mLoginRepository.bind(mApplication, data, customerId, productionLine);
    }

    public void list(int pageNum, int pageSize, String beginModifyTime, String endModifyTime) {
        if (mLoginRepository == null)
            mLoginRepository = new MRepository();
        mLoginRepository.list(pageNum, pageSize, beginModifyTime, endModifyTime);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mLoginRepository) {
            mLoginRepository.onDestroy();
            mLoginRepository = null;
        }
    }
}
